
bt.module({builder: function(bt){
    var j$ = jQuery;
    var cardScroller;
    var animation_int = setInterval("bt.misc.callNextCard(true)", 3000);

    var sns_type_name_mapping = {
        "sina": "新浪微博"
        //"tencent": "腾讯微博",
        //"renren": "人人网"
    };


    var callNextCard = function(auto) {
        if (j$(".navigator-panel .last-one").hasClass("current")) return;
        if (auto) {
            cardScroller.scrollNext();
        }
        var current_nav = j$(".user-summary-content .navigator-panel .nav-icon.current");
        var current_num = current_nav.data("card-num");
        current_nav.removeClass("current");
        j$(".user-summary-content .navigator-panel .nav-" + (current_num + 1)).addClass("current");
    };

    var markWaypoint = function(point) {
        var items = point.children();
        var lat = parseFloat($(items[0]).text());
        var lon = parseFloat($(items[1]).text());

        var wp_left = 800 / 360 * (180 + lon) - 10 - 25;
        var wp_top = 462 / 180 * (90 - lat) - 19 - 25; // 463 -> map's height
        var div = j$("<div></div>");
        div.addClass("waypoint-icon");
        div.css("top", wp_top);
        div.css("left", wp_left);
        j$(".user-summary-content .card-2 .world-map").append(div);
    };

    var initCardsPanel = function() {
        var photo_count = 1;
        cardScroller = new ScrollPanel(".card-panel", ".cards", {
            speed: 500,
            itemSize: 890,
            displayCount: photo_count}).navigator(".navi");
        cardScroller.addItem(j$(".card-panel .card").length);
    };

    var initNavigatorBar = function() {
        var target_div = j$(".user-summary-content .navigator-panel");
        var card_num = j$(".user-summary-content .card").length;
        for (var i = 0; i < card_num; i++) {
            var div = j$("<div></div>");
            div.addClass("nav-icon nav-" + i);
            div.attr("data-card-num", i);
            if (i == 0) {
                div.addClass("first-one current");
            } else if (i == card_num - 1) {
                div.addClass("last-one");
            }
            target_div.append(div);
        }
        target_div.css("padding-left", (890 - card_num * 22 + 10) / 2);
    };

    var connectSns = function(sns_type, sns_name) {
        sns_name = sns_name || sns_type_name_mapping[sns_type];
        alert("您还未绑定" + sns_name + "，请绑定后重试");
        if(sns_type == "sina") sns_type = "sina2";
        j$(location).attr("href", "/accounts/settings/connect_sns/");
    };

    var checkSNSBind = function() {
        j$.getJSON('/accounts/settings/sync_sns/status/?v=2', function(data){
            sns_data = data;
            var sns_type = "sina";
            var connected = data[sns_type];
            if(!connected) {
                connectSns(sns_type);
            } else {
                doShare();
            }
        });
    };

    var doShare = function() {
        var url = j$(".user-summary-content .card-11 .share-btn").data("share-url");
        var args = j$("#share-summary2012").serialize();
        $.ajax({
            type: "POST",
            url: url,
            data: args
        }).success(function() {
                alert("分享成功");
            })
    };

    $(document).ready(function() {
        initCardsPanel();
        initNavigatorBar();

        // Added waypoint icon on world map image
        var points = j$('#map_data').children();
        for (var i = 0; i < points.length; i++) {
            var point = j$(points[i]);
            markWaypoint(point);
        }

        // Resized popular photo
        j$(".user-summary-content .card-10 img").load(function() {
            bt.misc.centerAlignImg(j$(this), 380, 380, false);
        });
        bt.misc.centerAlignImg(j$(".user-summary-content .card-10 img"), 380, 380, false);

        j$(".user-summary-content .card-4 .start-place").ThreeDots({
            max_rows: 1,
            alt_text_t: true,
            whole_word: false
        });


        j$(".user-summary-content .navi .next").click(function() {
            callNextCard(false);
            clearInterval(animation_int);
        });
        j$(".user-summary-content .navi .prev").click(function() {
            if (j$(".navigator-panel .first-one").hasClass("current")) return;
            var current_nav = j$(".user-summary-content .navigator-panel .nav-icon.current");
            var current_num = current_nav.data("card-num");
            current_nav.removeClass("current");
            j$(".user-summary-content .navigator-panel .nav-" + (current_num - 1)).addClass("current");
            clearInterval(animation_int);
        });
        j$(".user-summary-content .navigator-panel .nav-icon").click(function() {
            var card_num = j$(this).data("card-num");
            cardScroller.scrollTo(card_num);
            j$(".user-summary-content .navigator-panel .nav-icon.current").removeClass("current");
            j$(this).addClass("current");
            clearInterval(animation_int);
        });

        // Init weibo share button event
        j$(".user-summary-content .card-11 .share-btn").click(function() {
            checkSNSBind();
        });
    });

    $(window).resize(function() {
    });

    bt.misc.callNextCard = callNextCard;
}});


